load("Data/survey_data.RData")

# Create empathy scales ------------
data$IRI_1 <- (data$IRI_part_1_1-1)/4
data$IRI_2 <- (data$IRI_part_1_2-1)/4
data$IRI_3 <- 1-(data$IRI_part_1_3-1)/4
data$IRI_4 <- 1-(data$IRI_part_1_4-1)/4
data$IRI_5 <- (data$IRI_part_1_5-1)/4
data$IRI_6 <- (data$IRI_part_1_6-1)/4
data$IRI_7 <- 1-(data$IRI_part_1_7-1)/4
data$IRI_8 <- (data$IRI_part_2_1-1)/4
data$IRI_9 <- (data$IRI_part_2_2-1)/4
data$IRI_10 <- (data$IRI_part_2_3-1)/4
data$IRI_11 <- (data$IRI_part_2_4-1)/4
data$IRI_12 <- 1-(data$IRI_part_2_5-1)/4
data$IRI_13 <- 1-(data$IRI_part_2_6-1)/4
data$IRI_14 <- 1-(data$IRI_part_3_1-1)/4
data$IRI_15 <- 1-(data$IRI_part_3_2-1)/4
data$IRI_16 <- (data$IRI_part_3_3-1)/4
data$IRI_17 <- (data$IRI_part_3_4-1)/4
data$IRI_18 <- 1-(data$IRI_part_3_5-1)/4
data$IRI_19 <- 1-(data$IRI_part_3_6-1)/4
data$IRI_20 <- (data$IRI_part_3_7-1)/4
data$IRI_21 <- (data$IRI_part_4_1-1)/4
data$IRI_22 <- (data$IRI_part_4_2-1)/4
data$IRI_23 <- (data$IRI_part_4_3-1)/4
data$IRI_24 <- (data$IRI_part_4_4-1)/4
data$IRI_25 <- (data$IRI_part_4_5-1)/4
data$IRI_26 <- (data$IRI_part_4_6-1)/4
data$IRI_27 <- (data$IRI_part_4_7-1)/4
data$IRI_28 <- (data$IRI_part_4_8-1)/4

data$empathic_concern <- rowMeans(data[,c("IRI_2" , "IRI_4" , "IRI_9", "IRI_14" , "IRI_18" , "IRI_20", "IRI_22")], na.rm=F)
data$perspective_taking <- rowMeans(data[,c("IRI_3" , "IRI_8" , "IRI_11" , "IRI_15" , "IRI_21" , "IRI_25" ,"IRI_28")], na.rm=F)
data$fantasy <- rowMeans(data[,c("IRI_1" , "IRI_5" , "IRI_7" , "IRI_12" , "IRI_16" , "IRI_23" , "IRI_26")], na.rm=F)
data$personal_distress <- rowMeans(data[,c("IRI_6" , "IRI_10" , "IRI_13" , "IRI_17" , "IRI_19" , "IRI_24" , "IRI_27")], na.rm=F)

data <- subset(data, select = -c(IRI_part_1_1, IRI_part_1_2,IRI_part_1_3,IRI_part_1_4,IRI_part_1_5,IRI_part_1_6,IRI_part_1_7,IRI_part_2_1,IRI_part_2_2,
                                IRI_part_2_3,IRI_part_2_4,IRI_part_2_5,IRI_part_2_6,IRI_part_4_1,IRI_part_4_2,IRI_part_4_3,IRI_part_4_4,IRI_part_4_5,IRI_part_4_6,
                                IRI_part_4_7,IRI_part_4_8,IRI_part_3_1,IRI_part_3_2,IRI_part_3_3,IRI_part_3_4,IRI_part_3_5,IRI_part_3_6,IRI_part_3_7,
                                IRI_1,IRI_2,IRI_3,IRI_4,IRI_5,IRI_6,IRI_7,IRI_8,IRI_9,IRI_10,
                                IRI_11,IRI_12,IRI_13,IRI_14,IRI_15,IRI_16,IRI_17,IRI_18,IRI_19,IRI_20,
                                IRI_21,IRI_22,IRI_23,IRI_24,IRI_25,IRI_26,IRI_27,IRI_28))

# Create dependent variables-------

# Note columns 6:24 should be the party sympathy_ columns

#SCK operatinalization
data$absolute_inparty_sympathy <- apply(data[6:24], MARGIN =  1, FUN = max)
data$absolute_outparty_sympathy <- apply(data[6:24], MARGIN =  1, FUN = min)
data$range_party_sympathy <- data$absolute_inparty_sympathy - data$absolute_outparty_sympathy 
data$mean_party_sympathy <- apply(data[6:24], MARGIN =  1, FUN = mean)

#weighted sympathy scores
data$W_PS_VVD <- data$Party_Sympathy_1*(34/150)
data$W_PS_D66 <- data$Party_Sympathy_2*(24/150)
data$W_PS_PVV <- data$Party_Sympathy_3*(17/150)
data$W_PS_CDA <- data$Party_Sympathy_4*(14/150)
data$W_PS_PvdA <- data$Party_Sympathy_5*(9/150)
data$W_PS_GL <- data$Party_Sympathy_6*(8/150)
data$W_PS_SP <- data$Party_Sympathy_7*(9/150)
data$W_PS_PvdD <- data$Party_Sympathy_8*(6/150)
data$W_PS_CU <- data$Party_Sympathy_9*(5/150)
data$W_PS_FvD <- data$Party_Sympathy_10*(5/150)
data$W_PS_SGP <- data$Party_Sympathy_11*(3/150)
data$W_PS_VOLT <- data$Party_Sympathy_12*(3/150)
data$W_PS_JA21 <- data$Party_Sympathy_13*(3/150)
data$W_PS_DENK <- data$Party_Sympathy_14*(3/150)
data$W_PS_GroepHaga <- data$Party_Sympathy_15*(3/150)
data$W_PS_BBB <- data$Party_Sympathy_16*(1/150)
data$W_PS_BIJ1 <- data$Party_Sympathy_17*(1/150)
data$W_PS_Omtzigt <- data$Party_Sympathy_18*(1/150)
data$W_PS_DenHaan <- data$Party_Sympathy_19*(1/150)

#weighted average sympathy scores
data$weighted_average_PS <- rowSums(data[,42:60])

#weighted difference to average sympathy scores
data$PS_VVD_sq_dif_mean <- (34/150)*(data$Party_Sympathy_1 - data$weighted_average_PS)^2
data$PS_D66_sq_dif_mean <- (24/150)*(data$Party_Sympathy_2 - data$weighted_average_PS)^2
data$PS_PVV_sq_dif_mean <- (17/150)*(data$Party_Sympathy_3 - data$weighted_average_PS)^2
data$PS_CDA_sq_dif_mean <- (14/150)*(data$Party_Sympathy_4 - data$weighted_average_PS)^2
data$PS_PvdA_sq_dif_mean <- (9/150)*(data$Party_Sympathy_5 - data$weighted_average_PS)^2
data$PS_GL_sq_dif_mean <- (8/150)*(data$Party_Sympathy_6 - data$weighted_average_PS)^2
data$PS_SP_sq_dif_mean <- (9/150)*(data$Party_Sympathy_7 - data$weighted_average_PS)^2
data$PS_PvdD_sq_dif_mean <- (6/150)*(data$Party_Sympathy_8 - data$weighted_average_PS)^2
data$PS_CU_sq_dif_mean <- (5/150)*(data$Party_Sympathy_9 - data$weighted_average_PS)^2
data$PS_FvD_sq_dif_mean <- (5/150)*(data$Party_Sympathy_10 - data$weighted_average_PS)^2
data$PS_SGP_sq_dif_mean <- (3/150)*(data$Party_Sympathy_11 - data$weighted_average_PS)^2
data$PS_VOLT_sq_dif_mean <- (3/150)*(data$Party_Sympathy_12 - data$weighted_average_PS)^2
data$PS_JA21_sq_dif_mean <- (3/150)*(data$Party_Sympathy_13 - data$weighted_average_PS)^2
data$PS_DENK_sq_dif_mean <- (3/150)*(data$Party_Sympathy_14 - data$weighted_average_PS)^2
data$PS_GroepHaga_sq_dif_mean <- (3/150)*(data$Party_Sympathy_15 - data$weighted_average_PS)^2
data$PS_BBB_sq_dif_mean <- (1/150)*(data$Party_Sympathy_16 - data$weighted_average_PS)^2
data$PS_BIJ1_sq_dif_mean <- (1/150)*(data$Party_Sympathy_17 - data$weighted_average_PS)^2
data$PS_Omtzigt_sq_dif_mean <- (1/150)*(data$Party_Sympathy_18 - data$weighted_average_PS)^2
data$PS_DenHaan_sq_dif_mean <- (1/150)*(data$Party_Sympathy_19 - data$weighted_average_PS)^2

#weighted difference to highest sympathy score
data$PS_VVD_sq_dif_inparty <- (34/150)*(data$Party_Sympathy_1 - data$absolute_inparty_sympathy)^2
data$PS_D66_sq_dif_inparty <- (24/150)*(data$Party_Sympathy_2 - data$absolute_inparty_sympathy)^2
data$PS_PVV_sq_dif_inparty <- (17/150)*(data$Party_Sympathy_3 - data$absolute_inparty_sympathy)^2
data$PS_CDA_sq_dif_inparty <- (14/150)*(data$Party_Sympathy_4 - data$absolute_inparty_sympathy)^2
data$PS_PvdA_sq_dif_inparty <- (9/150)*(data$Party_Sympathy_5 - data$absolute_inparty_sympathy)^2
data$PS_GL_sq_dif_inparty <- (8/150)*(data$Party_Sympathy_6 - data$absolute_inparty_sympathy)^2
data$PS_SP_sq_dif_inparty <- (9/150)*(data$Party_Sympathy_7 - data$absolute_inparty_sympathy)^2
data$PS_PvdD_sq_dif_inparty <- (6/150)*(data$Party_Sympathy_8 - data$absolute_inparty_sympathy)^2
data$PS_CU_sq_dif_inparty <- (5/150)*(data$Party_Sympathy_9 - data$absolute_inparty_sympathy)^2
data$PS_FvD_sq_dif_inparty <- (5/150)*(data$Party_Sympathy_10 - data$absolute_inparty_sympathy)^2
data$PS_SGP_sq_dif_inparty <- (3/150)*(data$Party_Sympathy_11 - data$absolute_inparty_sympathy)^2
data$PS_VOLT_sq_dif_inparty <- (3/150)*(data$Party_Sympathy_12 - data$absolute_inparty_sympathy)^2
data$PS_JA21_sq_dif_inparty <- (3/150)*(data$Party_Sympathy_13 - data$absolute_inparty_sympathy)^2
data$PS_DENK_sq_dif_inparty <- (3/150)*(data$Party_Sympathy_14 - data$absolute_inparty_sympathy)^2
data$PS_GroepHaga_sq_dif_inparty <- (3/150)*(data$Party_Sympathy_15 - data$absolute_inparty_sympathy)^2
data$PS_BBB_sq_dif_inparty <- (1/150)*(data$Party_Sympathy_16 - data$absolute_inparty_sympathy)^2
data$PS_BIJ1_sq_dif_inparty <- (1/150)*(data$Party_Sympathy_17 - data$absolute_inparty_sympathy)^2
data$PS_Omtzigt_sq_dif_inparty <- (1/150)*(data$Party_Sympathy_18 - data$absolute_inparty_sympathy)^2
data$PS_DenHaan_sq_dif_inparty <- (1/150)*(data$Party_Sympathy_19 - data$absolute_inparty_sympathy)^2

#weighted difference to lowest sympathy score
data$PS_VVD_sq_dif_outparty <- (34/150)*(data$Party_Sympathy_1 - data$absolute_outparty_sympathy)^2
data$PS_D66_sq_dif_outparty <- (24/150)*(data$Party_Sympathy_2 - data$absolute_outparty_sympathy)^2
data$PS_PVV_sq_dif_outparty <- (17/150)*(data$Party_Sympathy_3 - data$absolute_outparty_sympathy)^2
data$PS_CDA_sq_dif_outparty <- (14/150)*(data$Party_Sympathy_4 - data$absolute_outparty_sympathy)^2
data$PS_PvdA_sq_dif_outparty <- (9/150)*(data$Party_Sympathy_5 - data$absolute_outparty_sympathy)^2
data$PS_GL_sq_dif_outparty <- (8/150)*(data$Party_Sympathy_6 - data$absolute_outparty_sympathy)^2
data$PS_SP_sq_dif_outparty <- (9/150)*(data$Party_Sympathy_7 - data$absolute_outparty_sympathy)^2
data$PS_PvdD_sq_dif_outparty <- (6/150)*(data$Party_Sympathy_8 - data$absolute_outparty_sympathy)^2
data$PS_CU_sq_dif_outparty <- (5/150)*(data$Party_Sympathy_9 - data$absolute_outparty_sympathy)^2
data$PS_FvD_sq_dif_outparty <- (5/150)*(data$Party_Sympathy_10 - data$absolute_outparty_sympathy)^2
data$PS_SGP_sq_dif_outparty <- (3/150)*(data$Party_Sympathy_11 - data$absolute_outparty_sympathy)^2
data$PS_VOLT_sq_dif_outparty <- (3/150)*(data$Party_Sympathy_12 - data$absolute_outparty_sympathy)^2
data$PS_JA21_sq_dif_outparty <- (3/150)*(data$Party_Sympathy_13 - data$absolute_outparty_sympathy)^2
data$PS_DENK_sq_dif_outparty <- (3/150)*(data$Party_Sympathy_14 - data$absolute_outparty_sympathy)^2
data$PS_GroepHaga_sq_dif_outparty <- (3/150)*(data$Party_Sympathy_15 - data$absolute_outparty_sympathy)^2
data$PS_BBB_sq_dif_outparty <- (1/150)*(data$Party_Sympathy_16 - data$absolute_outparty_sympathy)^2
data$PS_BIJ1_sq_dif_outparty <- (1/150)*(data$Party_Sympathy_17 - data$absolute_outparty_sympathy)^2
data$PS_Omtzigt_sq_dif_outparty <- (1/150)*(data$Party_Sympathy_18 - data$absolute_outparty_sympathy)^2
data$PS_DenHaan_sq_dif_outparty <- (1/150)*(data$Party_Sympathy_19 - data$absolute_outparty_sympathy)^2

#Spread score for Affective Polarization
data$Spread_Affective_Polarization <- sqrt(rowSums(data[,62:80]))

#Weighted Distant Relative Inparty Favoritism (how much more one party is liked)
data$Distant_Inparty_favoritism <- sqrt(rowSums(data[,81:99]))

#Weighted Distant Relative Outparty Favoritism (how much more one party is disliked)
data$Distant_Outparty_favoritism <- sqrt(rowSums(data[,100:118]))

#unweighted difference to mean party sympathy
data$PS_VVD_unweighted_dif_mean <- (data$Party_Sympathy_1 - data$mean_party_sympathy)^2
data$PS_D66_unweighted_dif_mean <- (data$Party_Sympathy_2 - data$mean_party_sympathy)^2
data$PS_PVV_unweighted_dif_mean <- (data$Party_Sympathy_3 - data$mean_party_sympathy)^2
data$PS_CDA_unweighted_dif_mean <- (data$Party_Sympathy_4 - data$mean_party_sympathy)^2
data$PS_PvdA_unweighted_dif_mean <- (data$Party_Sympathy_5 - data$mean_party_sympathy)^2
data$PS_GL_unweighted_dif_mean <- (data$Party_Sympathy_6 - data$mean_party_sympathy)^2
data$PS_SP_unweighted_dif_mean <- (data$Party_Sympathy_7 - data$mean_party_sympathy)^2
data$PS_PvdD_unweighted_dif_mean <- (data$Party_Sympathy_8 - data$mean_party_sympathy)^2
data$PS_CU_unweighted_dif_mean <- (data$Party_Sympathy_9 - data$mean_party_sympathy)^2
data$PS_FvD_unweighted_dif_mean <- (data$Party_Sympathy_10 - data$mean_party_sympathy)^2
data$PS_SGP_unweighted_dif_mean <- (data$Party_Sympathy_11 - data$mean_party_sympathy)^2
data$PS_VOLT_unweighted_dif_mean <- (data$Party_Sympathy_12 - data$mean_party_sympathy)^2
data$PS_JA21_unweighted_dif_mean <- (data$Party_Sympathy_13 - data$mean_party_sympathy)^2
data$PS_DENK_unweighted_dif_mean <- (data$Party_Sympathy_14 - data$mean_party_sympathy)^2
data$PS_GroepHaga_unweighted_dif_mean <- (data$Party_Sympathy_15 - data$mean_party_sympathy)^2
data$PS_BBB_unweighted_dif_mean <- (data$Party_Sympathy_16 - data$mean_party_sympathy)^2
data$PS_BIJ1_unweighted_dif_mean <- (data$Party_Sympathy_17 - data$mean_party_sympathy)^2
data$PS_Omtzigt_unweighted_dif_mean <- (data$Party_Sympathy_18 - data$mean_party_sympathy)^2
data$PS_DenHaan_unweighted_dif_mean <- (data$Party_Sympathy_19 - data$mean_party_sympathy)^2

#unweighted Spread score for Affective Polarization
data$unweighted_spread_affective_polarization <- sqrt((rowSums(data[,122:140])/19))

#inparty grouping
data$sympathy_FarLeft <- rowMeans(data[,c("Party_Sympathy_7" , "Party_Sympathy_8", "Party_Sympathy_17")], na.rm=F)
data$sympathy_CenterLeft <- rowMeans(data[,c("Party_Sympathy_5" , "Party_Sympathy_6", "Party_Sympathy_14")], na.rm=F)
data$sympathy_Center <- rowMeans(data[,c("Party_Sympathy_2" , "Party_Sympathy_9", "Party_Sympathy_12")], na.rm=F)
data$sympathy_CenterRight <- rowMeans(data[,c("Party_Sympathy_1" , "Party_Sympathy_4", "Party_Sympathy_11", "Party_Sympathy_16", "Party_Sympathy_18", "Party_Sympathy_19")], na.rm=F)
data$sympathy_FarRight <- rowMeans(data[,c("Party_Sympathy_3" , "Party_Sympathy_10", "Party_Sympathy_13", "Party_Sympathy_15")], na.rm=F)

data$inparties_sympathy <- apply(data[,c("sympathy_FarLeft", "sympathy_CenterLeft", "sympathy_Center",
                                         "sympathy_CenterRight", "sympathy_FarRight")], MARGIN =  1, FUN = max)
data$outparties_sympathy <- apply(data[,c("sympathy_FarLeft", "sympathy_CenterLeft", "sympathy_Center",
                                          "sympathy_CenterRight", "sympathy_FarRight")], MARGIN =  1, FUN = min)

data$inparty[data$inparties_sympathy == data$sympathy_FarLeft &
               data$sympathy_FarLeft != data$sympathy_CenterLeft &
               data$sympathy_FarLeft != data$sympathy_Center &
               data$sympathy_FarLeft != data$sympathy_CenterRight &
               data$sympathy_FarLeft != data$sympathy_FarRight] = 'Far Left'
data$inparty[data$inparties_sympathy == data$sympathy_CenterLeft &
               data$sympathy_CenterLeft == data$sympathy_FarLeft &
               data$sympathy_CenterLeft != data$sympathy_Center &
               data$sympathy_CenterLeft != data$sympathy_CenterRight &
               data$sympathy_CenterLeft != data$sympathy_FarRight] = 'Left'
data$inparty[data$inparties_sympathy == data$sympathy_CenterLeft &
               data$sympathy_CenterLeft != data$sympathy_FarLeft &
               data$sympathy_CenterLeft != data$sympathy_Center &
               data$sympathy_CenterLeft != data$sympathy_CenterRight &
               data$sympathy_CenterLeft != data$sympathy_FarRight] = 'Mainstream Left'
data$inparty[data$inparties_sympathy == data$sympathy_Center &
               data$sympathy_Center != data$sympathy_FarLeft &
               data$sympathy_Center == data$sympathy_CenterLeft &
               data$sympathy_Center != data$sympathy_CenterRight &
               data$sympathy_Center != data$sympathy_FarRight] = 'Center Left leaning'
data$inparty[data$inparties_sympathy == data$sympathy_Center &
               data$sympathy_Center != data$sympathy_FarLeft &
               data$sympathy_Center != data$sympathy_CenterLeft &
               data$sympathy_Center != data$sympathy_CenterRight &
               data$sympathy_Center != data$sympathy_FarRight] = 'Center'
data$inparty[data$inparties_sympathy == data$sympathy_Center &
               data$sympathy_Center != data$sympathy_FarLeft &
               data$sympathy_Center != data$sympathy_CenterLeft &
               data$sympathy_Center == data$sympathy_CenterRight &
               data$sympathy_Center != data$sympathy_FarRight] = 'Center Right leaning'
data$inparty[data$inparties_sympathy == data$sympathy_CenterRight &
               data$sympathy_CenterRight != data$sympathy_CenterLeft &
               data$sympathy_CenterRight != data$sympathy_Center &
               data$sympathy_CenterRight != data$sympathy_CenterRight &
               data$sympathy_CenterRight != data$sympathy_FarRight] = 'Mainstream Right'
data$inparty[data$inparties_sympathy == data$sympathy_CenterRight &
               data$sympathy_CenterRight != data$sympathy_CenterLeft &
               data$sympathy_CenterRight != data$sympathy_Center &
               data$sympathy_CenterRight != data$sympathy_CenterRight &
               data$sympathy_CenterRight == data$sympathy_FarRight] = 'Right'
data$inparty[data$inparties_sympathy == data$sympathy_FarRight &
               data$sympathy_FarRight != data$sympathy_CenterLeft &
               data$sympathy_FarRight != data$sympathy_Center &
               data$sympathy_FarRight != data$sympathy_CenterRight &
               data$sympathy_FarRight != data$sympathy_FarLeft] = 'Far Right'
data$inparty[data$inparties_sympathy == data$sympathy_Center &
               data$sympathy_Center != data$sympathy_FarLeft &
               data$sympathy_Center == data$sympathy_CenterLeft &
               data$sympathy_Center == data$sympathy_CenterRight &
               data$sympathy_Center != data$sympathy_FarRight] = 'Mainstream'
data$inparty[data$inparties_sympathy == data$sympathy_FarLeft &
               data$sympathy_FarLeft != data$sympathy_FarLeft &
               data$sympathy_FarLeft != data$sympathy_CenterLeft &
               data$sympathy_FarLeft != data$sympathy_CenterRight &
               data$sympathy_FarLeft == data$sympathy_FarRight] = 'Radical'
data$inparty[is.na(data$inparty) ] = 'No inparty group'

data$outparty[data$outparties_sympathy == data$sympathy_FarLeft &
                data$sympathy_FarLeft != data$sympathy_CenterLeft &
                data$sympathy_FarLeft != data$sympathy_Center &
                data$sympathy_FarLeft != data$sympathy_CenterRight &
                data$sympathy_FarLeft != data$sympathy_FarRight] = 'Far Left'
data$outparty[data$outparties_sympathy == data$sympathy_CenterLeft &
                data$sympathy_CenterLeft == data$sympathy_FarLeft &
                data$sympathy_CenterLeft != data$sympathy_Center &
                data$sympathy_CenterLeft != data$sympathy_CenterRight &
                data$sympathy_CenterLeft != data$sympathy_FarRight] = 'Left'
data$outparty[data$outparties_sympathy == data$sympathy_CenterLeft &
                data$sympathy_CenterLeft != data$sympathy_FarLeft &
                data$sympathy_CenterLeft != data$sympathy_Center &
                data$sympathy_CenterLeft != data$sympathy_CenterRight &
                data$sympathy_CenterLeft != data$sympathy_FarRight] = 'Maoutstream Left'
data$outparty[data$outparties_sympathy == data$sympathy_Center &
                data$sympathy_Center != data$sympathy_FarLeft &
                data$sympathy_Center == data$sympathy_CenterLeft &
                data$sympathy_Center != data$sympathy_CenterRight &
                data$sympathy_Center != data$sympathy_FarRight] = 'Center Left leaning'
data$outparty[data$outparties_sympathy == data$sympathy_Center &
                data$sympathy_Center != data$sympathy_FarLeft &
                data$sympathy_Center != data$sympathy_CenterLeft &
                data$sympathy_Center != data$sympathy_CenterRight &
                data$sympathy_Center != data$sympathy_FarRight] = 'Center'
data$outparty[data$outparties_sympathy == data$sympathy_Center &
                data$sympathy_Center != data$sympathy_FarLeft &
                data$sympathy_Center != data$sympathy_CenterLeft &
                data$sympathy_Center == data$sympathy_CenterRight &
                data$sympathy_Center != data$sympathy_FarRight] = 'Center Right leaning'
data$outparty[data$outparties_sympathy == data$sympathy_CenterRight &
                data$sympathy_CenterRight != data$sympathy_CenterLeft &
                data$sympathy_CenterRight != data$sympathy_Center &
                data$sympathy_CenterRight != data$sympathy_CenterRight &
                data$sympathy_CenterRight != data$sympathy_FarRight] = 'Maoutstream Right'
data$outparty[data$outparties_sympathy == data$sympathy_CenterRight &
                data$sympathy_CenterRight != data$sympathy_CenterLeft &
                data$sympathy_CenterRight != data$sympathy_Center &
                data$sympathy_CenterRight != data$sympathy_CenterRight &
                data$sympathy_CenterRight == data$sympathy_FarRight] = 'Right'
data$outparty[data$outparties_sympathy == data$sympathy_FarRight &
                data$sympathy_FarRight != data$sympathy_CenterLeft &
                data$sympathy_FarRight != data$sympathy_Center &
                data$sympathy_FarRight != data$sympathy_CenterRight &
                data$sympathy_FarRight != data$sympathy_FarLeft] = 'Far Right'
data$outparty[data$outparties_sympathy == data$sympathy_Center &
                data$sympathy_Center != data$sympathy_FarLeft &
                data$sympathy_Center == data$sympathy_CenterLeft &
                data$sympathy_Center == data$sympathy_CenterRight &
                data$sympathy_Center != data$sympathy_FarRight] = 'Maoutstream'
data$outparty[data$outparties_sympathy == data$sympathy_FarLeft &
                data$sympathy_FarLeft != data$sympathy_FarLeft &
                data$sympathy_FarLeft != data$sympathy_CenterLeft &
                data$sympathy_FarLeft != data$sympathy_CenterRight &
                data$sympathy_FarLeft == data$sympathy_FarRight] = 'Radical'
data$outparty[is.na(data$outparty) ] = 'No outparty group'

data$Voted[data$TK2021=='VVD']  <- "VVD"
data$Voted[data$TK2021=='PVV (Partij voor de Vrijheid)']  <- "PVV"
data$Voted[data$TK2021=='CDA']  <- "CDA"
data$Voted[data$TK2021=='D66']  <- "D66"
data$Voted[data$TK2021=='GROENLINKS']  <- "GL"
data$Voted[data$TK2021=='SP (Socialistische Partij)']  <- "SP"
data$Voted[data$TK2021=='Partij van de Arbeid (P.v.d.A.)']  <- "PvdA"
data$Voted[data$TK2021=='ChristenUnie']  <- "CU"
data$Voted[data$TK2021=='Partij voor de Dieren']  <- "PvdD"
data$Voted[data$TK2021=='Staatkundig Gereformeerde Partij (SGP)']  <- "SGP"
data$Voted[data$TK2021=='Forum voor Democratie']  <- "FvD"
data$Voted[data$TK2021=='JA21']  <- "JA21"
data$Voted[data$TK2021=='Volt']  <- "VOLT"

#making long datasets based on voter-party dyads
names(data)[names(data) == "Party_Sympathy_1"] <- "VVD"
names(data)[names(data) == "Party_Sympathy_2"] <- "D66"
names(data)[names(data) == "Party_Sympathy_3"] <- "PVV"
names(data)[names(data) == "Party_Sympathy_4"] <- "CDA"
names(data)[names(data) == "Party_Sympathy_5"] <- "PvdA"
names(data)[names(data) == "Party_Sympathy_6"] <- "GL"
names(data)[names(data) == "Party_Sympathy_7"] <- "SP"
names(data)[names(data) == "Party_Sympathy_8"] <- "PvdD"
names(data)[names(data) == "Party_Sympathy_9"] <- "CU"
names(data)[names(data) == "Party_Sympathy_10"] <- "FvD"
names(data)[names(data) == "Party_Sympathy_11"] <- "SGP"
names(data)[names(data) == "Party_Sympathy_12"] <- "VOLT"
names(data)[names(data) == "Party_Sympathy_13"] <- "JA21"
names(data)[names(data) == "Party_Sympathy_14"] <- "DENK"
names(data)[names(data) == "Party_Sympathy_15"] <- "GroepHaga"
names(data)[names(data) == "Party_Sympathy_16"] <- "BBB"
names(data)[names(data) == "Party_Sympathy_17"] <- "BIJ1"
names(data)[names(data) == "Party_Sympathy_18"] <- "Omtzigt"
names(data)[names(data) == "Party_Sympathy_19"] <- "DenHaan"

data = subset(data, select = -c(W_PS_VVD, W_PS_D66, W_PS_PVV, W_PS_CDA, W_PS_PvdA, W_PS_GL, W_PS_SP, W_PS_PvdD, 
                                W_PS_CU, W_PS_FvD, W_PS_SGP, W_PS_VOLT, W_PS_JA21, W_PS_DENK, W_PS_GroepHaga, 
                                W_PS_BBB, W_PS_BIJ1, W_PS_Omtzigt, W_PS_DenHaan,
                                
                                PS_VVD_sq_dif_mean, PS_D66_sq_dif_mean, PS_PVV_sq_dif_mean,PS_CDA_sq_dif_mean,
                                PS_PvdA_sq_dif_mean, PS_GL_sq_dif_mean,PS_SP_sq_dif_mean,PS_PvdD_sq_dif_mean,
                                PS_CU_sq_dif_mean,PS_FvD_sq_dif_mean,PS_SGP_sq_dif_mean,PS_VOLT_sq_dif_mean,
                                PS_JA21_sq_dif_mean,PS_DENK_sq_dif_mean,PS_GroepHaga_sq_dif_mean,PS_BBB_sq_dif_mean,
                                PS_BIJ1_sq_dif_mean,PS_Omtzigt_sq_dif_mean,PS_DenHaan_sq_dif_mean,
                                
                                PS_VVD_sq_dif_inparty, PS_D66_sq_dif_inparty, PS_PVV_sq_dif_inparty,PS_CDA_sq_dif_inparty,
                                PS_PvdA_sq_dif_inparty, PS_GL_sq_dif_inparty,PS_SP_sq_dif_inparty,PS_PvdD_sq_dif_inparty,
                                PS_CU_sq_dif_inparty,PS_FvD_sq_dif_inparty,PS_SGP_sq_dif_inparty,PS_VOLT_sq_dif_inparty,
                                PS_JA21_sq_dif_inparty,PS_DENK_sq_dif_inparty,PS_GroepHaga_sq_dif_inparty,PS_BBB_sq_dif_inparty,
                                PS_BIJ1_sq_dif_inparty,PS_Omtzigt_sq_dif_inparty,PS_DenHaan_sq_dif_inparty,
                                
                                PS_VVD_sq_dif_outparty, PS_D66_sq_dif_outparty, PS_PVV_sq_dif_outparty,PS_CDA_sq_dif_outparty,
                                PS_PvdA_sq_dif_outparty, PS_GL_sq_dif_outparty,PS_SP_sq_dif_outparty,PS_PvdD_sq_dif_outparty,
                                PS_CU_sq_dif_outparty,PS_FvD_sq_dif_outparty,PS_SGP_sq_dif_outparty,PS_VOLT_sq_dif_outparty,
                                PS_JA21_sq_dif_outparty,PS_DENK_sq_dif_outparty,PS_GroepHaga_sq_dif_outparty,PS_BBB_sq_dif_outparty,
                                PS_BIJ1_sq_dif_outparty,PS_Omtzigt_sq_dif_outparty,PS_DenHaan_sq_dif_outparty,
                                
                                PS_VVD_unweighted_dif_mean, PS_D66_unweighted_dif_mean, PS_PVV_unweighted_dif_mean,PS_CDA_unweighted_dif_mean,
                                PS_PvdA_unweighted_dif_mean, PS_GL_unweighted_dif_mean,PS_SP_unweighted_dif_mean,PS_PvdD_unweighted_dif_mean,
                                PS_CU_unweighted_dif_mean,PS_FvD_unweighted_dif_mean,PS_SGP_unweighted_dif_mean,PS_VOLT_unweighted_dif_mean,
                                PS_JA21_unweighted_dif_mean,PS_DENK_unweighted_dif_mean,PS_GroepHaga_unweighted_dif_mean,PS_BBB_unweighted_dif_mean,
                                PS_BIJ1_unweighted_dif_mean,PS_Omtzigt_unweighted_dif_mean,PS_DenHaan_unweighted_dif_mean,
                                mean_party_sympathy, weighted_average_PS))

data$Distant_Outparty_disfavoritism <- 10-data$Distant_Outparty_favoritism
data$absolute_outparty_UNsympathy <-10-data$absolute_outparty_sympathy

#----Control Variables-----
#news exposure
#changing label for news exposure
names(data)[2] <- 'news_exposure_1'
names(data)[3] <- 'news_exposure_2'
names(data)[4] <- 'news_exposure_3'

data$news_exposure <- apply(data[c("news_exposure_1","news_exposure_2","news_exposure_3")], MARGIN =  1, FUN = min)
data$news_exposure_mean <- apply(data[c("news_exposure_1","news_exposure_2","news_exposure_3")], MARGIN =  1, FUN = mean)

#political leaning
data$left_right_placement <- data$ideology_1 - 5

#ideological extremism
data$ideological_extremism <- sqrt(data$left_right_placement^2)

save(data, file = "Data/study_1.RData")
